package com.example.student.controller;

import com.example.student.pojo.PageBean;
import com.example.student.pojo.Result;
import com.example.student.pojo.Stu;
import com.example.student.service.ScoreService;
import com.example.student.service.StuService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 学生信息管理Controller
 */
@Slf4j
@RestController

public class StuController {
    @Autowired
    private StuService stuService;

    //查询所有学生信息
    @GetMapping("/students")
    public Result list() {
        log.info("查询所有数据");
        List<Stu> stulist=stuService.list();
        return Result.success(stulist);
    }

    //根据给出的id删除学生信息
    @DeleteMapping("/student/{id}")
    public Result delete(@PathVariable Integer id) {
        log.info("根据{}删除学生信息",id);
        stuService.delete(id);
        return Result.success();}

    //根据返回的集合批量删除
    @DeleteMapping("/students/{ids}")
    public Result deletes(@PathVariable String ids) {
        log.info("根据{}删除学生信息",ids);
        List<Integer> idList = Arrays.stream(ids.split(","))
                .map(Integer::parseInt)
                .toList();
        stuService.deletes(idList);
        return Result.success();
    }

    //修改学生信息
    @GetMapping("/students/{id}")
    public Result getByID(@PathVariable Integer id) {
        log.info("获取学生ID:{}",id);
        Stu stu = stuService.getByID(id);
        return Result.success(stu);
    }
    @PutMapping("/students")
    public Result update(@RequestBody Stu stu) {
        log.info("修改学生信息:{}",stu);
        stuService.update(stu);
        return Result.success();
    }

    //添加学生信息
    @PostMapping("/students")
    public Result addDept(@RequestBody Stu stu) {
        log.info("新增学生:{}", stu);
        return stuService.add(stu);
    }

    //分页查询所有学生信息
    @GetMapping("/allStudents")
    public Result page(@RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "10") Integer pageSize) {
        PageBean pageBean=stuService.page(page,pageSize);
        return Result.success(pageBean);
    }
    //根据条件查询学生信息
    @GetMapping("/findStudents")
    public Result page(@RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "10") Integer pageSize,
                       Integer studentId,String name,Short gender) {
        PageBean pageBean=stuService.pages(page,pageSize,studentId,name,gender);
        return Result.success(pageBean);
    }
    //根据班级查询学生信息
    @GetMapping("/classStudents")
    public Result page(@RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "10") Integer pageSize,Integer deptId) {
        PageBean pageBean=stuService.classPage(page,pageSize,deptId);
        return Result.success(pageBean);
    }
}
